package com.luocj.looook.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.luocj.looook.R;
import com.luocj.looook.utils.DensityUtil;


/**
 * Created by luocj515 on 2017/11/27.
 * 项目名称：LooooK
 * 类描述：
 * 创建人：hasee
 * 创建时间：2017/11/27 15:30
 * 修改备注：
 */

public class CircleView extends View {
    private int width;//宽度
    private int height;//高度
    private int radius1 = DensityUtil.dp2px(100);//半径
    private Paint redPaint;
    private Paint yellowPaint;
    private float paintWidth = DensityUtil.dp2px(5);
    private int textSize = DensityUtil.dp2px(25);
    private int mRound;
    private int mWidth;
    private Rect bounds = new Rect();
    private Paint publicPaint = new Paint();
    private Rect textBounds = new Rect();
    private int sweetAngle = 0;

    public CircleView(Context context) {
        this(context, null);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initArrts(context, attrs);

        initPaint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(width / 2, height / 2, radius1 - paintWidth / 2, redPaint);

        RectF rectF = new RectF(paintWidth / 2, paintWidth / 2, width - paintWidth / 2, height - paintWidth / 2);
        canvas.drawArc(rectF, 90, sweetAngle, false, yellowPaint);
        drawText(canvas, 100);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        final int top = 0 + getPaddingTop();
        final int bottom = getHeight() - getPaddingBottom();
        final int left = 0 + getPaddingLeft();
        final int right = getWidth() - getPaddingRight();
        updateBounds(top, bottom, left, right);
//        initArrowPath(top, bottom, left, right, getRadius());
    }

    private void updateBounds(int top, int bottom, int left, int right) {
        bounds.set(left, top, right, bottom);
    }

    private void drawText(Canvas canvas, float progress_degree) {
//        final String sDegree = String.valueOf(Math.round(progress_degree * 100));
        publicPaint.setStyle(Paint.Style.FILL);
        publicPaint.setTextSize(textSize);
        publicPaint.setTextAlign(Paint.Align.CENTER);
        Paint.FontMetricsInt fontMetrics = publicPaint.getFontMetricsInt();
        int baseline = (bounds.bottom + bounds.top - fontMetrics.bottom - fontMetrics.top) / 2;
        canvas.drawText("nihao", bounds.centerX(), baseline, publicPaint);
//        publicPaint.getTextBounds("nihao", 0, "nihao".length(), textBounds);
//        publicPaint.setTextSize(textSize);
//        publicPaint.setTextAlign(Paint.Align.LEFT);
//        canvas.drawText("%", rect.centerX() + textBounds.width() / 2 + .1f * radius1, baseline, publicPaint);
    }

    private void initArrts(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleView);
        mRound = typedArray.getDimensionPixelSize(R.styleable.CircleView_round, 0);
        mWidth = typedArray.getDimensionPixelSize(R.styleable.CircleView_width, 0);
        radius1 = typedArray.getDimensionPixelSize(R.styleable.CircleView_radius1, radius1);
        textSize = typedArray.getInteger(R.styleable.CircleView_text_size, textSize);
        typedArray.recycle();
    }

    private void initPaint() {
        redPaint = new Paint();
        redPaint.setColor(Color.RED);
        redPaint.setStyle(Paint.Style.STROKE);
        redPaint.setStrokeCap(Paint.Cap.BUTT);
        redPaint.setAntiAlias(true);
        redPaint.setStrokeWidth(paintWidth);

        yellowPaint = new Paint();
        yellowPaint.setColor(Color.YELLOW);
        yellowPaint.setStyle(Paint.Style.STROKE);
        yellowPaint.setStrokeCap(Paint.Cap.BUTT);
        yellowPaint.setAntiAlias(true);
        yellowPaint.setStrokeWidth(paintWidth);

        publicPaint.setStrokeCap(Paint.Cap.ROUND);
        publicPaint.setStrokeWidth(width);
        publicPaint.setStyle(Paint.Style.STROKE);
        publicPaint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(widthMeasureSpec);

        // 在宽高不是精确模式时,定义最小宽高

        if (widthMode != MeasureSpec.EXACTLY) {
//            width = getDisplayMetrics(getContext()).widthPixels;
            width = DensityUtil.dp2px(200);
        }

        if (heightMode != MeasureSpec.EXACTLY) {
            height = DensityUtil.dp2px(200);
        }
        setMeasuredDimension(width, height);
    }

    public int getSweetAngle() {
        return sweetAngle;
    }

    public void setSweetAngle(int angle) {
        this.sweetAngle = angle;
        invalidate();
//        Log.i("123", "setSweetAngle: " + sweetAngle);
    }
}
